﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

namespace UI
{
    public class RecycleSelectPanel : MonoBehaviour, ISubContent
    {
        public GameObject scrollView;
        public SimpleGrid simpleGrid;
        public GameObject itemPrefab;
        public Transform cacheTrans;

        public List<UITweener> tweeners;

        protected List<GameObject> cachedObjs = new List<GameObject>();

        bool isShow = false;
        public bool IsShow()
        {
            return isShow;
        }

        public void SetShow(bool isShow)
        {
            this.isShow = isShow;
            gameObject.SetActive(true);
        }


        public void ShowTab()
        {
            gameObject.SetActive(true);
            ShowTab0();

        }

        public virtual void ShowTab0()
        {
            InitSelectCache();

            InitItems();
        }

        public void ReShowTab()
        {
        }

        public void CloseTab()
        {
            gameObject.SetActive(false);
            foreach (var obj in simpleGrid.childs)
            {
                cachedObjs.Add(obj);
                obj.transform.SetParent(cacheTrans);
            }
            simpleGrid.childs.Clear();
            cacheTrans.gameObject.SetActive(false);
        }

        public void OnRemoveCoverPage()
        {
        }

        public void OnPageClose()
        {
            gameObject.SetActive(false);
            foreach (var obj in simpleGrid.childs)
            {
                cachedObjs.Add(obj);
                obj.transform.SetParent(cacheTrans);
            }
            simpleGrid.childs.Clear();
            cacheTrans.gameObject.SetActive(false);
            ClearSelectCache();
        }

        public virtual GameObject CreateSelectItem(object obj)
        {
            return GameObject.Instantiate(itemPrefab) as GameObject;
        }

        public virtual void InitItems()
        {
            List<GameObject> objs = new List<GameObject>();
            foreach (var item in GetCanSelectItems())
            {
                GameObject obj = CreateSelectItem(item);
                obj.GetComponent<RecycleSelectItemBase>().Init(item, OnRecycleResetSelectItemClick);
                if (IsItemSelected(item))
                {
                    obj.GetComponent<RecycleSelectItemBase>().Select(true);
                    objs.Insert(0, obj);
                }
                else
                {
                    obj.GetComponent<RecycleSelectItemBase>().Select(false);
                    objs.Add(obj);
                }
            }
            foreach (var obj in cachedObjs)
            {
                GameObject.Destroy(obj);
            }
            cachedObjs.Clear();
            SortItem(objs);
            foreach (var obj in objs)
            {
                simpleGrid.AddChild(obj);
            }
        }

        public virtual void SortItem(List<GameObject> objs)
        {

        }

        public virtual void ShowSelectMaxTip() { }

        public virtual void OnRecycleResetSelectItemClick(GameObject obj)
        {
            bool isSelected = obj.GetComponent<RecycleSelectItemBase>().IsSelected;
            string guid = obj.GetComponent<RecycleSelectItemBase>().GetItemGUID();
            if (isSelected)
            {
                RemoveSelectCache(guid);
                obj.GetComponent<RecycleSelectItemBase>().Select(false);
            }
            else
            {
                if (IsSelectMax())
                {
                    ShowSelectMaxTip();
                    return;
                }
                else {
                    AddSelectCache(guid);
                    obj.GetComponent<RecycleSelectItemBase>().Select(true);
                }
            }
        }

        public virtual bool IsSelectMax()
        {
            return false;
        }

        public virtual void RemoveSelectCache(string guid)
        {

        }

        public virtual void AddSelectCache(string guid)
        {

        }

        public virtual void ClearSelectCache()
        {

        }

        public virtual void InitSelectCache()
        {

        }

        public virtual bool IsItemSelected(object item)
        {
            return false;
        }

        public virtual List<object> GetCanSelectItems()
        {
            return new List<object>();

        }

        public virtual void OnOpenSubPageTweenOver()
        {
            
        }


        public virtual void OnConfirmButtonClick()
        {
        }
    }
}
